<template>
	<view class="container">
		<form>
			<view class="tui-box" style="padding-top: 0;">
				<view class="tui-item-box"
					style="padding: 14px 16px;box-sizing: border-box;background-color: #fcf4d0;color: #94711c;"
					@tap="choosecatid">
					<text class="tui-list-cell_name" style="font-size: 26rpx;">你选择的服务类别是：{{cat_name || '请先选择分类'}}</text>
					<view class="tui-right" style="font-weight: 600;">></view>
				</view>

				<tui-list-cell :hover="false" :lineLeft="false" padding="0">
					<view class="tui-remark-box tui-padding tui-flex">
						<!-- <view>您的要求</view> -->
						<input type="text" @input="remarkInput" name="remark" class="tui-remark" placeholder="请输入您的要求"
							placeholder-class="tui-phcolor"></input>
					</view>
				</tui-list-cell>
				<tui-list-cell :hover="false" :lineLeft="false" padding="0">
					<view class="tui-remark-box tui-padding tui-flex"
						style="flex-direction: column;align-items: flex-start;">
						<view style="margin-bottom: 20rpx;" v-if="publicIsPic == 1">
							<tui-upload :value="imgvalue" :limit="5" @complete="uploadresult"
								@remove="remove"></tui-upload>
						</view>
						<view v-if="publicIsVideo == 1">
							<image v-show="!videoSrc" style="width: 120rpx;height: 120rpx;"
								src="../../static/images/vedio.png" mode="" @click="uploadVedio"></image>
							<view class="video" style="position: relative;width: 120rpx;height: 120rpx;"
								v-if="videoSrc">
								<view class="" style="position:absolute;right:-18px;top:-18px" @click="deleteVideo">
									<tui-icon name="shut" :size="20" color="red"></tui-icon>
								</view>
								<video style="width: 120rpx;height: 120rpx;" :src="videoSrc"></video>
							</view>
						</view>
					</view>
				</tui-list-cell>

				<tui-list-cell :hover="false" :lineLeft="false" padding="0">
					<view class="tui-remark-box tui-padding" style="border-top: 20rpx solid #f3f3f3;">
						<view style="font-weight: 600;">付款方式</view>
						<!-- <input type="text" v-model="total" name="remark" style="text-align: right;" placeholder="请输入您的期望价格"
							placeholder-class="tui-phcolor"></input> -->
						<view style="margin-top: 30rpx;">
							<radio-group v-model="type" @change="onTypeChange">
								<radio :checked="type === '1'" style="margin: 0 5rpx 0 0" value="1" />
								参与竞标
								<radio :checked="type === '2'" style="margin: 0 5rpx 0 20rpx" value="2" />
								一口价
								<radio :checked="type === '3'" style="margin: 0 5rpx 0 20rpx" value="3" />
								上门估价
							</radio-group>
						</view>
						<view v-if="type === '1'" class="text">
							参与竞标，客户预约时，一次性支付本服务的全部费用。商家承诺，本服务无额外收费。
						</view>
						<view v-if="type === '2'" class="text">
							客户预约时，一次性支付本服务的全部费用。商家承诺，本服务无额外收费。
						</view>
						<view v-if="type === '3'" class="text">
							上门费：用户下单时将预付上门费，商家按约定时间上门后，商家可收取上门费，商家可对改服务进行报价，如若客户同意报价，商家可获取新的服务费用。
						</view>
					</view>
				</tui-list-cell>

				<tui-list-cell :hover="false" :lineLeft="false" padding="0" v-if="type === '1'">
					<view class="tui-remark-box tui-padding tui-flex">
						<view style="font-weight: 600;">预算</view>
						<input type="text" v-model="total" name="remark" style="text-align: right;"
							placeholder="请输入您的期望价格" placeholder-class="tui-phcolor"></input>
					</view>
				</tui-list-cell>

				<tui-list-cell :hover="false" :lineLeft="false" padding="0" v-if="type === '2'">
					<view class="tui-remark-box tui-padding tui-flex">
						<view style="font-weight: 600;">价格</view>
						<input type="text" v-model="total" name="remark" style="text-align: right;" :disabled="true"
							placeholder="请输入您的期望价格" placeholder-class="tui-phcolor"></input>
					</view>
				</tui-list-cell>

				<tui-list-cell :hover="false" :lineLeft="false" padding="0" v-if="type === '3'">
					<view class="tui-remark-box tui-padding tui-flex">
						<view style="font-weight: 600;">上门费用</view>
						<input type="text" v-model="total" name="remark" style="text-align: right;" :disabled="true"
							placeholder="请输入您的期望价格" placeholder-class="tui-phcolor"></input>
					</view>
				</tui-list-cell>

				<tui-list-cell :hover="false" :lineLeft="false" padding="0">
					<view class="tui-remark-box tui-padding tui-flex">
						<view style="font-weight: 600;font-size: 28rpx;color: #000;">市场参考价</view>
						<input type="text" v-model="reference_price" disabled name="remark"
							style="text-align: right;color: red;" placeholder="请输入您的期望价格"
							placeholder-class="tui-phcolor"></input>
					</view>
				</tui-list-cell>


				<!-- 			
				<tui-list-cell  v-if="type === '1'" :arrow="true" unlined
					:radius="true" padding="0" @click="chooseTime">
					<view class="tui-address" style="padding: 0 30rpx">
						<view class="tui-none-addr">
							<text style="font-weight: 600;">竞标有效期</text>
						</view>
					</view>
				</tui-list-cell> -->

				<tui-list-cell v-if="(deliverymode==1 || deliverymode==4) && is_times==0" :arrow="true" unlined
					:radius="true" padding="0" @click="chooseTime">
					<view class="tui-address" style="padding: 0 30rpx">
						<view v-if="servicetime">
							<view class="tui-addr">
								<text>期望上门时间:{{servicetime}}</text>
							</view>
						</view>
						<view class="tui-none-addr" v-else>
							<text style="font-weight: 600;">服务时间</text>
						</view>
					</view>
				</tui-list-cell>
				<!-- <tui-list-cell :arrow="true" unlined :radius="true" @click="chooseAddr" padding="0"  style="border-top: 20rpx solid #f3f3f3;">
					<view class="tui-address" style="padding: 0 30rpx;">
						<label class="serviceaddr" style="font-weight: 600;color: #333;">服务地址</label>
						<view v-if="curAddressData.address">
							<view class="tui-addr">
								<text>{{curAddressData.address_default}}{{curAddressData.address}}</text>
							</view>
						</view>
						<view class="tui-none-addr" v-else>
							<image src="/static/images/location_fill.png" class="tui-addr-img" mode="widthFix"></image>
							<text>请选择地址</text>
						</view>
					</view>
				</tui-list-cell> -->

				<view class="" style="border-top: 20rpx solid #f3f3f3;">
					<tui-list-cell :arrow="true" unlined :radius="true" @click="chooseAddr" padding="0">
						<view class="tui-address" style="padding: 0 30rpx;">
							<label class="serviceaddr" style="font-weight: 600;color: #333;">获取位置</label>
							<view v-if="curAddressData.address">
								<!-- <view class="tui-userinfo">
									<text class="tui-name">{{curAddressData.name}}</text>{{curAddressData.telephone}}
								</view> -->
								<view class="tui-addr">
									<text>{{curAddressData.address_default}}{{curAddressData.address}}</text>
								</view>
							</view>
							<view class="tui-none-addr" v-else>
								<image src="/static/images/location_fill.png" class="tui-addr-img" mode="widthFix">
								</image>
								<text>请点击获取位置</text>
							</view>
						</view>
					</tui-list-cell>
					<!-- <tui-list-cell unlined :radius="true" @click="chooseAddr" padding="0">
						<view class="tui-remark-box tui-padding tui-flex">
							<view style="font-weight: 600;">详细地址</view>
							<input type="text" v-model="address" name="remark" style="text-align: right;"
								placeholder="请输入详细地址" placeholder-class="tui-phcolor"></input>
						</view>
					</tui-list-cell>
					<tui-list-cell unlined :radius="true" padding="0">
						<view class="tui-remark-box tui-padding tui-flex">
							<view style="font-weight: 600;">联系人</view>
							<input type="text" v-model="people" name="remark" style="text-align: right;"
								placeholder="请填写联系人姓名" placeholder-class="tui-phcolor"></input>
						</view>
					</tui-list-cell>
					<tui-list-cell unlined :radius="true" padding="0">
						<view class="tui-remark-box tui-padding tui-flex">
							<view style="font-weight: 600;">联系方式</view>
							<input type="text" v-model="phone" name="remark" style="text-align: right;"
								placeholder="请填写手机号" placeholder-class="tui-phcolor"></input>
						</view>

					</tui-list-cell> -->
				</view>

				<!-- <tui-list-cell :hover="false">
					<view class="tui-img__title">添加图片</view>
					<view>
						<tui-upload :value="imgvalue" :limit="5" @complete="uploadresult" @remove="remove"></tui-upload>
					</view>
				</tui-list-cell> -->
			</view>
			<view class="tui-safe-area"></view>
			<view class="tui-tabbar" v-if="type != '1'">
				<view class="tui-flex-end tui-color-red tui-pr-20">
					<view class="tui-black">应付金额: </view>
					<view class="tui-size-26">￥</view>
					<view class="tui-price-large">{{total}}</view>
					<!--<view class="tui-size-26"></view>-->
				</view>
				<view class="tui-pr25">
					<tui-button width="380rpx" height="70rpx" :size="28" type="danger" shape="circle" @click="btnPay">
						确认提交
					</tui-button>
				</view>
			</view>
			<view class="tui-tabbar" v-else>
				<view class="tui-pr25">
					<tui-button width="380rpx" height="70rpx" :size="28" type="danger" shape="circle"
						@click="createOrder(false)">
						确认提交
					</tui-button>
				</view>
			</view>
			<t-pay-way :show="show" :amuont="total" @goPay="goPay" :paymethod="paymethod" @close="popupClose">
			</t-pay-way>
		</form>
	</view>
</template>

<script>
	import tPayWay from "@/components/views/t-pay-way/t-pay-way"
	import pay from '@/common/pay.js'
	export default {
		components: {
			tPayWay
		},
		data() {
			return {
				type1: '0',
				technicalId: '',
				show: false,
				islogin: 1, //是否要需要登录，需要在onLoad加截 app.page.onLoad(this,e);
				servicetime: '',
				cat_id: '',
				cat_name: '',
				curAddressData: [],
				total: 0,
				remark: "",
				orderType: "", //订单类型，购物车下单或立即支付下单，默认是购物车，
				selIndex: 0,
				payment: '0', //是否支持货到付款
				chargeset: '0', //是否开启服务费
				paymentType: "",
				deliverymode: '1',
				is_times: 0,
				ptype: 2,
				service: 0,
				paymethod: [],
				orderimage: [],
				visit_fee: 0,
				reference_price: 0,
				type: '1',
				imgvalue: [], //初始化图片
				videoSrc: '',
				publicIsVideo: 0,
				publicIsPic: 0
			}
		},
		onLoad: function(e) {
			let _this = this
			if (e.orderType) {
				_this.orderType = e.orderType;
			}
			_this.$request.get('Order.paymethod').then(res => {
				if (res.errno == 0) {
					console.log(res)
					_this.paymethod = res.data;
					_this.paymentType = res.data[0].default;
					//console.log(_this.paymentType);
				}
			})
			_this.$request.get('Order.visit_fee').then(res => {
				if (res.errno == 0) {
					console.log(res);
					_this.visit_fee = parseFloat(res.data)
				}
			})
		},
		onShow: function() {
			let _this = this
			_this.servicetime = uni.getStorageSync('servicetime');
			_this.cat_id = uni.getStorageSync('cat_id');
			_this.cat_name = uni.getStorageSync('cat_name');
			_this.reference_price = parseFloat(uni.getStorageSync('cat_price'));
			_this.initShippingAddress();
			_this.getCommonCofig();
		},
		methods: {
			getCommonCofig() {
				this.$request.post('config', {
					mo: 'common'
				}).then(res => {
					console.log(res.data)
					if (res.errno == 0) {
						if (res.data.public_is_pic != undefined) {
							this.publicIsPic = res.data.public_is_pic;
						}
						if (res.data.public_is_video != undefined) {
							this.publicIsVideo = res.data.public_is_video;
						}
					}
				});
			},
			deleteVideo() {
				this.videoSrc = ''
				console.log('12121')
			},
			uploadVedio() {
				let self = this;
				uni.chooseVideo({
					sourceType: ['camera'],
					success: function(res) {
						return new Promise((resolve, reject) => {
							self.$request.uploadFile(res.tempFilePath).then(e => {
								self.videoSrc = e.url
							});
						})
					}
				});
			},
			onTypeChange(val) {
				this.type = val.target.value
				if (this.type == 2) {
					this.total = this.reference_price;
				} else if (this.type == 3) {
					this.total = this.visit_fee;
				} else {
					this.total = 0;
				}
			},
			uploadresult: function(e) {
				console.log(e)
				this.orderimage = e.imgArr;
			},
			remove: function(e) {
				//移除图片
				console.log(e)
				let index = e.index
			},
			chooseAddr() {
				uni.navigateTo({
					url: "/pagesA/my/address/address"
				})
			},
			chooseTime() {
				uni.navigateTo({
					url: "/pages/submitOrder/timelist"
				})
			},
			choosecatid() {
				uni.navigateTo({
					url: "/pages/submitOrder/cat"
				})
			},
			btnPay() {
				this.show = true
			},
			popupClose() {
				this.show = false
			},
			goPay(e) {
				if (e.paymentType) {
					this.paymentType = e.paymentType;
				}
				this.createOrder(true);
			},
			createOrder: function(is_pay) {
				var _this = this;
				if (_this.remark == undefined) {
					_this.remark = '';
				}

				if (!_this.cat_id) {
					uni.hideLoading();
					uni.showModal({
						title: '错误',
						content: '请选择需求分类！',
						showCancel: false
					})
					return;
				}

				if (_this.total == 0 && is_pay == true) {
					uni.hideLoading();
					uni.showModal({
						title: '错误',
						content: '期望价格不能为0！',
						showCancel: false
					})
					return;
				}

				if (_this.paymentType == 0) {
					uni.hideLoading();
					uni.showModal({
						title: '错误',
						content: '请选择支付方式！',
						showCancel: false
					})
					return;
				}
				let postData = {
					technicalId: _this.technicalId,
					cat_id: _this.cat_id,
					remark: _this.remark,
					price_type: _this.type,
					total: _this.total,
					paymentType: _this.paymentType,
					orderimage: _this.orderimage,
					videoSrc: _this.videoSrc
				};
				postData.payment = postData.paymentType;
				postData.is_times = _this.is_times;

				if (_this.deliverymode == 1) {
					if (_this.is_times == 0) {
						if (!_this.servicetime) {
							uni.hideLoading();
							uni.showModal({
								title: '错误',
								content: '请选择期望上门时间！',
								showCancel: false
							})
							return;
						}
						postData.servicetime = _this.servicetime;
					}
					postData.address_id = _this.curAddressData.id;
					_this.ptype = 2;
				}

				_this.$request.post('order.create', postData).then(res => {
					if (res.errno != 0) {
						uni.showModal({
							title: '错误',
							content: res.msg,
							showCancel: false
						})
						return;
					}

					const redirectUrl = "/pages/submitOrder/success?price_type=" + _this.type + "&ptype=" +
						_this.ptype;

					if (is_pay == true) {
						if (res.data.payment_code == 'wx_pay') {
							pay.wxpay('order', res.data.pay_total, res.data.id, redirectUrl);
						} else if (res.data.payment_code == 'tt_pay') {
							pay.ttpay('order', res.data.pay_total, res.data.id, redirectUrl);
						} else if (res.data.payment_code == 'hee_pay') {
							pay.heepay('order', res.data.pay_total, res.data.id, redirectUrl);
						} else if (res.data.payment_code == 'balance_pay') {
							_this.$request.post('order.pay', {
								orderid: res.data.id
							}).then(res => {
								if (res.errno == 0) {
									wx.showModal({
										title: '成功',
										content: '使用余额支付成功',
										showCancel: false,
										success: function(res) {
											wx.redirectTo({
												url: redirectUrl
											});
										}
									})
								} else {
									wx.showModal({
										title: '失败',
										content: res.msg,
										showCancel: false
									})
								}
							})
						} else if (res.data.payment_code == 'offline_pay') {
							_this.sam.navigateTo('/pages/submitOrder/offlinepayment?id=' + res.data.id);
						} else if (res.data.payment_code == 'delivery_pay') {
							_this.sam.navigateTo(redirectUrl);
						} else {
							_this.sam.navigateTo(redirectUrl);
						}
					} else {
						_this.sam.navigateTo(redirectUrl);
					}
				})
			},
			initShippingAddress: function() {
				var _this = this;
				_this.$request.get('address.default', {
					samkey: (new Date()).valueOf()
				}).then(res => {
					if (res.errno == 0) {
						_this.curAddressData = res.data;
					} else {
						_this.curAddressData = null;
					}
				})
			},
			remarkInput: function(e) {
				this.remark = e.target.value;
			},

		}
	}
</script>

<style>
	.container {
		padding-bottom: 98rpx;
	}

	.tui-item-box {
		width: 100%;
		display: flex;
		align-items: center;
	}

	.tui-list-cell_name {
		padding-left: 5rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.tui-right {
		margin-left: auto;
		margin-right: 34rpx;
		font-size: 26rpx;
		color: #999;
	}

	.tui-box {
		padding: 20rpx 0 118rpx;
		box-sizing: border-box;
	}

	.tui-address {
		min-height: 80rpx;
		padding: 10rpx 0;
		box-sizing: border-box;
		position: relative;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.serviceaddr {
		color: #999999;
	}

	.tui-userinfo {
		font-size: 30rpx;
		font-weight: 500;
		line-height: 30rpx;
		padding-top: 10rpx;
		padding-bottom: 12rpx;
	}

	.tui-name {
		padding-right: 40rpx;
	}

	.tui-addr {
		font-size: 24rpx;
		word-break: break-all;
		padding-right: 25rpx;
	}

	.tui-addr-tag {
		padding: 5rpx 8rpx;
		flex-shrink: 0;
		background: #EB0909;
		color: #fff;
		display: inline-flex;
		align-items: center;
		justify-content: center;
		font-size: 25rpx;
		line-height: 25rpx;
		transform: scale(0.8);
		transform-origin: 0 center;
		border-radius: 6rpx;
	}

	.tui-top {
		margin-top: 20rpx;
		overflow: hidden;
	}

	.tui-goods-title {
		font-size: 28rpx;
		display: flex;
		align-items: center;
	}

	.tui-padding {
		box-sizing: border-box;
	}

	.tui-goods-item {
		width: 100%;
		padding: 20rpx 30rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
	}

	.tui-goods-img {
		width: 180rpx;
		height: 180rpx;
		display: block;
		flex-shrink: 0;
	}

	.tui-goods-center {
		flex: 1;
		padding: 20rpx 8rpx;
		box-sizing: border-box;
	}

	.tui-goods-name {
		max-width: 310rpx;
		word-break: break-all;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		font-size: 26rpx;
		line-height: 32rpx;
	}

	.tui-goods-attr {
		font-size: 22rpx;
		color: #888888;
		line-height: 32rpx;
		padding-top: 20rpx;
		word-break: break-all;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
	}

	.tui-price-right {
		text-align: right;
		font-size: 24rpx;
		color: #888888;
		line-height: 30rpx;
		padding-top: 20rpx;
	}

	.tui-flex {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 26rpx;
	}

	.tui-total-flex {
		justify-content: flex-end;
	}

	.tui-color-red {
		color: #E41F19;
		padding-right: 30rpx;
	}

	.tui-balance {
		font-size: 28rpx;
		font-weight: 500;
	}

	.tui-black {
		color: #222;
		line-height: 30rpx;
	}

	.tui-gray {
		color: #888888;
		font-weight: 400;
	}

	.tui-light-dark {
		color: #666;
	}

	.tui-goods-price {
		display: flex;
		align-items: center;
		padding-top: 20rpx;
	}

	.tui-size-26 {
		font-size: 26rpx;
		line-height: 26rpx;
	}

	.tui-price-large {
		font-size: 34rpx;
		line-height: 32rpx;
		font-weight: 600;
	}

	.tui-flex-end {
		display: flex;
		align-items: flex-end;
		padding-right: 0;
	}

	.tui-phcolor {
		color: #B3B3B3;
		font-size: 26rpx;
	}

	/* #ifndef H5 */
	.tui-remark-box {
		padding: 22rpx 30rpx;
	}

	/* #endif */
	/* #ifdef H5 */
	.tui-remark-box {
		padding: 26rpx 30rpx;
	}

	/* #endif */

	.tui-remark {
		flex: 1;
		font-size: 26rpx;
		/* padding-left: 64rpx; */
	}

	.tui-scale-small {
		transform: scale(0.8);
		transform-origin: 100% center;
	}

	.tui-scale-small .wx-switch-input {
		margin: 0 !important;
	}

	/* #ifdef H5 */
	>>>uni-switch .uni-switch-input {
		margin-right: 0 !important;
	}

	/* #endif */
	.tui-tabbar {
		width: 100%;
		height: 98rpx;
		background: #fff;
		position: fixed;
		left: 0;
		bottom: 0;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		font-size: 26rpx;
		box-shadow: 0 0 1px rgba(0, 0, 0, .3);
		padding-bottom: env(safe-area-inset-bottom);
		z-index: 996;
	}

	.tui-pr-30 {
		padding-right: 30rpx;
	}

	.tui-pr-20 {
		padding-right: 20rpx;
	}

	.tui-none-addr {
		height: 80rpx;
		padding-left: 5rpx;
		display: flex;
		align-items: center;
	}

	.tui-addr-img {
		width: 36rpx;
		height: 46rpx;
		display: block;
		margin-right: 15rpx;
	}


	.tui-pr25 {
		padding-right: 25rpx;
	}

	.tui-safe-area {
		height: 1rpx;
		padding-bottom: env(safe-area-inset-bottom);
	}

	.tui-pay-item__title {
		width: 100%;
		height: 90rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 20rpx;
		box-sizing: border-box;
		font-size: 28rpx;
	}

	.tui-pay-amuont {
		color: #eb0909;
		font-weight: 500;
		font-size: 34rpx;
	}

	.tui-pay-item {
		width: 100%;
		height: 80rpx;
		display: flex;
		align-items: center;
		padding: 0 20rpx;
		box-sizing: border-box;
		font-size: 28rpx;
	}

	.tui-pay-logo {
		width: 48rpx;
		height: 48rpx;
		margin-right: 15rpx;
	}

	.tui-radio {
		margin-left: auto;
		transform: scale(0.8);
		transform-origin: 100% center;
	}

	.tui-btn-pay {
		width: 100%;
		padding: 68rpx 60rpx 50rpx;
		box-sizing: border-box;
	}

	.tui-recharge {
		color: #fc872d;
		margin-left: auto;
		padding: 12rpx 0;
	}

	.acea-row {
		font-size: 28rpx;
		padding-top: 30rpx;
		padding-left: 20rpx;
		height: 60rpx;
		background-color: #fff;
	}

	.acea-row label {
		padding: 10rpx;
	}

	.tui-img__title {
		padding-bottom: 24rpx;
	}
</style>
<style scoped>
	.type .tui-list-cell {
		background: red !important;
	}

	.text {
		font-size: 24rpx;
		margin: 10rpx 0;
	}
</style>